package cn.rkylin.oms.system.shop.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.common.consts.YesNoConst;
import cn.rkylin.oms.system.shop.dao.IShopStorDAO;
import cn.rkylin.oms.system.shop.domain.ShopStor;
import cn.rkylin.oms.system.shop.vo.ShopStorVO;

/**
 * 店铺仓库服务实现
 *
 */
@Service("shopStorService")
public class ShopStorServiceImpl implements IShopStorService {

    @Autowired
    protected IDataBaseFactory dao;

    @Autowired
    private IShopStorDAO shopStorDAO;

    /**
     * 根据条件获取 shopStorList
     */
    @Override
    public List<ShopStorVO> getShopStorByCondition(ShopStorVO shopStorVO) throws Exception {
        List<ShopStorVO> returnList = dao.find("selectConditionShopStor", shopStorVO);
        return returnList;
    }

    /**
     * 给店铺添加1个仓库
     * 
     * @param shopStor
     */
    @Override
    public int insertShopStor(ShopStor shopStor) throws Exception {
        return shopStorDAO.insert(shopStor);
    }

    /**
     * 删除1个店铺仓库
     * 
     * @param shopStorId
     * @throws Exception
     */
    @Override
    @Transactional
    public int deleteShopStor(String shopStorId) throws Exception {
        ShopStorVO shopStor = new ShopStorVO();
        shopStor.setShopStorId(shopStorId);
        shopStor.setDeleted(YesNoConst.YES);
        
        ShopStor shopStorTemp = shopStorDAO.selectByPrimaryKey(shopStorId);
        
        // 清除店铺的默认退货店铺
        ShopStor param = new ShopStorVO();
        param.setShopId(shopStorTemp.getShopId());
        param.setPrjId(shopStorTemp.getPrjId());
        shopStorDAO.updateCleanDefaultReturnStorByShopStor(param);
        
        return shopStorDAO.updateByPrimaryKeySelective(shopStor);
    }

    /**
     * 获取店铺的所有仓库
     * 
     * @param shopId
     * @throws Exception
     */
    @Override
    public List<ShopStorVO> getShopStorList(String shopId) throws Exception {
        ShopStor param = new ShopStor();
        param.setShopId(shopId);
        return dao.find("selectConditionShopStor", param);
    }

    @Override
    public List<ShopStorVO> getShopStorListByPrjId(String prjId) throws Exception {
        if (StringUtils.isBlank(prjId)){
            return null;
        }
//        ShopStorVO param = new ShopStorVO();
//        param.setPrjId(prjId);
        return dao.find("selectShopStorInfo", prjId);
    }


    @Override
    public List<ShopStorVO> getShopStorListByShopStor(ShopStorVO vo) throws Exception {
        return dao.find("selectShopStorInfoByShopStor", vo);
    }
    /**
     * 更新店铺仓库
     */
    @Override
    public int updateShopStor(ShopStor param) throws Exception {
        int returnValue = 0;
        if(StringUtils.isNotEmpty(param.getShopStorId())&&param.getShopStorId().contains(",")) {
            String[] shopStorIdArray = param.getShopStorId().split(",");
            for (String shopStorId : shopStorIdArray) {
                ShopStorVO shopStor = new ShopStorVO();
                BeanUtils.copyProperties(param, shopStor);
                shopStor.setShopStorId(shopStorId);
                // 更新店铺仓库的默认退货仓库
                returnValue+=shopStorDAO.updateByPrimaryKeySelective(param);
            }
        }else {
            returnValue = shopStorDAO.updateByPrimaryKeySelective(param);
        }
        return returnValue;
    }
}
